from flask import Blueprint,render_template, request, flash, session
from sqlalchemy import or_

from ..database import User, db
from ..utils import calc_md5


bp = Blueprint('register', __name__, url_prefix="/register")


@bp.route('/', methods=["POST","GET"])
def register():
    if request.method == "POST":
        name = request.form.get("account")
        pwd0 = request.form.get("password_0")
        pwd1 = request.form.get("password_1")
        print(name,pwd1, pwd0)
        if not bool(name):
            flash("注册账号不能为空")
        if not bool(pwd0):
            flash("密码不能为空")
        if pwd0 != pwd1:
            flash("密码不一致")

        u_exsit = User.query.filter_by(name=name).first()
        if bool(u_exsit):
            flash("用户已经存在")

        else:
            user = User(name=name,e_password=calc_md5(pwd1))
            db.session.add(user)
            db.session.commit()
            flash("注册成功")
            


    return render_template('/pages/register.html')



